import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_fancy_tree_view/flutter_fancy_tree_view.dart';
import 'package:power_study_language_tool/screen/player_screen.dart';

class FileListScreen extends StatefulWidget {
  const FileListScreen({super.key});

  @override
  State<FileListScreen> createState() => _FileListScreenState();
}

class _FileListScreenState extends State<FileListScreen> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [buildLeftPanel(), buildRightPanel()],
    );
  }

  buildLeftPanel() {
    MyTreeNode node1 = new MyTreeNode(title: "文件夹1");
    MyTreeNode node2 = new MyTreeNode(title: "文件1");
    MyTreeNode node3 = new MyTreeNode(title: "文件夹2");
    node1.children.add(node2);
    node1.children.add(node3);

    final List<MyTreeNode> roots = [
      // const MyTreeNode(title: 'root node'),
      node1
    ];

    final treeController = TreeController<MyTreeNode>(
      roots: roots,
      childrenProvider: (MyTreeNode node) => node.children,
    );
    return Container(
        width: 300,
        decoration: BoxDecoration(color: Colors.green),
        child: Column(
          children: [
            Container(
              height: 50,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  IconButton(
                      onPressed: () {}, icon: Icon(Icons.add_box_outlined)),
                  IconButton(onPressed: () {}, icon: Icon(Icons.delete)),
                ],
              ),
            ),
            Expanded(
              child: AnimatedTreeView<MyTreeNode>(
                treeController: treeController,
                nodeBuilder:
                    (BuildContext context, TreeEntry<MyTreeNode> entry) {
                  return Material(
                    child: InkWell(
                      onTap: () => treeController.toggleExpansion(entry.node),
                      child: TreeIndentation(
                        entry: entry,
                        child: Text(entry.node.title),
                      ),
                    ),
                  );
                },
              ),
            ),
          ],
        ));
  }

  buildRightPanel() {
    return Expanded(
        child: Material(
            child: ListView.builder(
                itemCount: 8,
                itemExtent: 50.0, //强制高度为50.0
                itemBuilder: (BuildContext context, int index) {
                  return Container(
                    height: 50,
                    child: Row(
                      children: [
                        Text("Unit${index + 1}"),
                        TextButton(
                            onPressed: () {
                              op(index);
                            },
                            child: Text("开始学习"))
                      ],
                    ),
                  );
                })));
  }

  void op(int index) {
    String mp4Path = "";
    String srtPath = "";
    if (index == 0) {
      mp4Path = "/Users/qiang/资料/英文/译林英语/unit1/unit1storytime.mp4";
      srtPath = "/Users/qiang/资料/英文/译林英语/unit1/unit1storytime.srt";
    } else if (index == 1) {
      mp4Path = "/Users/qiang/资料/英文/译林英语/unit2/unit2storytime.mp4";
      srtPath = "/Users/qiang/资料/英文/译林英语/unit2/unit2storytime.srt";
    }
    Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) =>
              PlayerScreen(mp4Path: mp4Path, srtPath: srtPath)),
    );
  }
}

class MyTreeNode {
  MyTreeNode({
    required this.title,
    //   this.children = <MyTreeNode>[],
  });

  String title;
  List<MyTreeNode> children = <MyTreeNode>[];
}
